import { User, UserProfile } from '@/types/data'
import { UserAction } from '@/types/store'

type UserState = {
  user: User
  profile: UserProfile
}

const initialState = {
  user: {},
  profile: {}
} as UserState

// user 数据，页面使用的数据，是这里处理好的。
export const user = (state = initialState, action: UserAction): UserState => {
  if (action.type === 'user/getUser') {
    return {
      ...state,
      user: action.payload
    }
  }
  // 用户资料
  if (action.type === 'user/getUserProfile') {
    return {
      ...state,
      profile: action.payload
    }
  }
  // 更新用户信息
  if (action.type === 'user/updateProfile') {
    return {
      ...state,
      profile: {
        ...state.profile,
        ...action.payload
      }
    }
  }
  return state
}
